package com.glory.dgcpcpda.activity.technicalservice;

import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.EqpAlarmReleaseAdapter;
import com.glory.dgcpcpda.adapter.MaterialBoxBindingAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.glory.dgcpcpda.entity.EqpAlarmRelease;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.MerterialBoxBindingEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: ShiftExchangeTestActivity
 * Author: XJH
 * Date: 2021/6/24 15:58
 * Description:
 * History:
 */
public class EqpAlarmReleaseActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private EqpAlarmReleaseAdapter eqpAlarmReleaseAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_alarm_release;
    }

    @Override
    protected void initView() {
        tvTitle.setText("预警解扣");
        setMainLayout(llMain);

        initRecyclerView(rvLotInfoList,this);
        tvBaseSubmit.setText("提交");
        eqpAlarmReleaseAdapter = new EqpAlarmReleaseAdapter(null);
        rvLotInfoList.setAdapter(eqpAlarmReleaseAdapter);

        initEdit(etLot, s -> {
            //获取lot信息
            FreeUI_EntityCallBack<BaseResponseBean<EqpAlarmRelease>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpAlarmRelease>>
                    (new TypeToken<BaseResponseBean<EqpAlarmRelease>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpAlarmRelease>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                        eqpAlarmReleaseAdapter.getData().clear();
                        eqpAlarmReleaseAdapter.addData(response.body().getResponse().getBody().getALARMLIST());
                    }

                }
                @Override
                public void onError(Response<BaseResponseBean<EqpAlarmRelease>> response) {
                    super.onError(response);
                    loadError(response.getException(), "OPPORTUNITYLOGEVENT");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("EQUIPMENTID", getText(etLot));
            map.addProperty("ACTIONTYPE","Query");
            NetUtils.requestPostNet("","ALMLIST", this, map, callBack);
        });
    }
    @Override
    protected void initData() {
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tvBaseSubmit,R.id.tvClear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if(eqpAlarmReleaseAdapter.getData().size()<=0){
                    showToast("批次列表不能为空", false);
                    return;
                }
                if(eqpAlarmReleaseAdapter.releaseData.size()<=0){
                    showToast("请勾选解扣数据", false);
                    return;
                }
                if(eqpAlarmReleaseAdapter.releaseData.size()>1){
                    showToast("只能勾选一条数据", false);
                    return;
                 }
                FreeUI_EntityCallBack<BaseResponseBean<EqpAlarmRelease>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpAlarmRelease>>
                        (new TypeToken<BaseResponseBean<EqpAlarmRelease>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<EqpAlarmRelease>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showConfirmDialog("解锁成功");
                            eqpAlarmReleaseAdapter.getData().clear();
                            eqpAlarmReleaseAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<EqpAlarmRelease>> response) {
                        super.onError(response);
                        loadError(response.getException(),"TRANSFERMES");

                    }
                };
                JsonObject map = new JsonObject();
                EqpAlarmRelease eqpAlarmRelease=eqpAlarmReleaseAdapter.getData().get(0);
                map.addProperty("ACTIONTYPE","UnLock");
                map.addProperty("EQUIPMENTID",eqpAlarmRelease.getEQP_NO());
                map.addProperty("AlarmCode", eqpAlarmRelease.getALARM_CODE());
                NetUtils.requestPostNet("","ALMLIST", this, map, callBack);
                break;
            case R.id.tvClear:
                eqpAlarmReleaseAdapter.getData().clear();
                eqpAlarmReleaseAdapter.notifyDataSetChanged();
                clearContent();
                break;

        }
    }

    //初始化页面
    private void clearContent() {
        etLot.setText("");
    }
}
